/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package proyecto.so1;

import java.net.*;
import java.io.*;

/**
 *
 * @author edwin
 */
public class Sync extends Thread implements Runnable{
    
    private boolean execute;
    private int port;
    
    public Sync(){
        execute = true;
        port = 10100;
    }
    
    
    @Override
    public void run(){
        try{
            ServerSocket server = new ServerSocket(port, 0, NetworkInterface.getByName("eth0").getInterfaceAddresses().get(1).getAddress());
            server.setReuseAddress(true);
            while(execute){
                Socket client = server.accept();
                OutputStream output = client.getOutputStream();
                DataOutputStream data = new DataOutputStream(output);
                File dir = new File("/var/grupo7");
                File[] list = dir.listFiles();
                data.writeInt(list.length-1);
                for(int k=0; k<list.length; k++){
                    File temp = list[k];
                    if(temp.isFile()){
                        data.writeUTF(temp.getName());
                        int length = (int)temp.length();
                        data.writeInt(length);
                        FileInputStream input = new FileInputStream(temp);
                        int paquetes = length/1024;
                        byte[] leidos;
                        if(paquetes <= 1){
                            leidos = new byte[length-1];
                            input.read(leidos, 0, length-1);
                            data.write(leidos, 0, length-1);
                        }
                        else{
                            int offset = 0;
                            for(int i=0; i<paquetes; i++){
                                int end = offset+1023;
                                if(end>length){
                                    leidos = new byte[length-offset-1];
                                    input.read(leidos, offset, length-1);
                                    data.write(leidos, offset, length-1);
                                }
                                else{
                                    leidos = new byte[1024];
                                    input.read(leidos, offset, end);
                                    data.write(leidos, offset, end);
                                }
                            }
                        }
                        input.close();
                    }
                }
                ObjectOutputStream out=new ObjectOutputStream(new BufferedOutputStream(data));
                out.writeObject(principal.states);
                out.close();
                output.close();
                client.close();
            }
        }
        catch(UnknownHostException uhe){ System.err.println(uhe.getMessage()); }
        catch(SocketException se){ System.err.println(se.getMessage()); }
        catch(IOException ioe){ System.err.println(ioe.getMessage()); }
    }
    
}
